/**
 * @author Kevin.MOU
 * Login view script Support
 */
(function($) {
	var Login = {
		onSubmit : function(password, remoteKey) {
			var a = this;
			// if (! password) {
			// 	showError('密码不可为空');
			// 	return;
			// }

			busy(1, $F.lang('submit'));
			Ajax.post({
				url : 'quicklogin_',
				data : {
					quickLoginId: a.quickId,
					password: $F.encPwd(password, a),
					uid: a.uid
				},
				success : function(data) {
					//console.log(data);
					if (data.code < 0) {
						switch(data.code) {
						case -7:
							// 重复登录
							showError(data.message);
							break;
						case -101:	// 密码为空
							showError('密码状态异常，禁止登录');
							break;
						case -102:	// 密码过期
							showError('密码过期，禁止登录');
							break;
						case -103:	// 密码过期（无修改时间）
							showError('密码过期，禁止登录');
							break;
						default:
							showError(data.message);
						}
						busy(0);
					} else {
						$('#vid').hide();
						location.href = Login.url + data.message;
					}
				},
				fail : function(jqXHR, textStatus, errorThrown) {
					showError(textStatus);
					busy(0);
				}
			});
		}
	};
	window.Login = Login;

	var busy = function(e, m) {
		if (e == 1) {
			$(".login_btn").hide();
			$(".login_busy").show().find('>span').text(m);
		} else {
			$(".login_busy").hide().find('>span').text('');
			$(".login_btn").show();
		}
	},

	showError = function(e) {
		$("#err_tips").text(e);
		$(".login_err_panel").show();
	},

	resizeBackground = function() {
		var bg = $('#bgDiv'),
			vid = $('#vid'),
			docHeight = $('body').innerHeight(),
			docWidth = $('body').innerWidth(),
			rate = 1.777777777777778;

		if (docWidth / docHeight > rate) {
			var vh = docWidth / rate;
			if (vh > docHeight) {
				bg.css({
					left: 0,
					top: (docHeight - vh) /2 + 'px'
				})
			} else {
				bg.css({
					left: 0,
					top: 0
				})
			}
			bg.css({
				width: docWidth,
				height: vh
			})
		} else {
			var vw = docHeight * rate;
			if (vw > docWidth) {
				bg.css({
					left: (docWidth - vh) /2 + 'px',
					top: 0
				})
			} else {
				bg.css({
					left: 0,
					top: 0
				})
			}
			bg.css({
				width: vw,
				height: docHeight
			})
		}
	};

	$(function() {
		var bg = $('#bgDiv'), vid = $('#vid'), img = new Image();
		$(window).on('resize', function(e){
			resizeBackground()
		}).trigger('resize');

		img.src = Login.url + '/images/background.jpg';
		img.onload = function() {
			bg.css({
				opacity: 1,
				backgroundImage : 'url(' + img.src + ')'
			})
		}
		vid.bind('canplay', function() {
			vid.css({opacity: 0.5})
		})

		Login.rsaPubKey = $('input#rsaPubKey').val();
		var form = $('form.login_form');
		form.on('submit', function(e) {
			e.preventDefault();

			$(".login_err_panel").hide();
			var password = $("input#password", form).val();
			let remoteKey = localStorage.getItem("elite_remote_key");
			if (!remoteKey) {
				remoteKey = Guid.raw();
				localStorage.setItem("elite_remote_key", remoteKey);
			}
			Login.onSubmit(password, remoteKey);
			return false;
		});

		var loginBox = $('.login_box');
		loginBox.css('margin-top', loginBox.outerHeight() / -2);
		$("input#password", form).focus();
	});
})(jQuery);
